﻿using CarAppDomain.CarType.IService;
using CarAppDto.CarType.BrandDtos;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using D.UtilCore;
using D.UtilSqlsugarOrm;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CarAppAPI.CarType
{
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class BrandController : BaseApiController
    {
        private readonly IBrandService _service;
        public BrandController(IBrandService service)
        {
            _service = service;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddBrand reqModel)
        {
            reqModel.CreateUser = Account;
            return await _service.AddBrandAsync(reqModel);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _service.DeleteBrandAsync(idsArray.ToList());
        }

        /// <summary>
        /// 修改
        /// </summary> 
        /// <param name="reqModel"></param>
        /// <returns></returns>
        [HttpPut, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update([FromBody] ReqUpdateBrand reqModel)
        {
            reqModel.UpdateUser = Account;
            return await _service.UpdateBrandAsync(reqModel);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepBrand>> Get(string? id)
        {
            return await _service.GetBrandAsync(id);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepBrand>>> List([FromBody] ReqQueryBrandList reqModel)
        {
            return await _service.QueryBrandListAsync(reqModel);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepBrand>>> Pages([FromBody] ReqQueryBrandPage reqQueryPage)
        {
            return await _service.QueryBrandPagesAsync(reqQueryPage);
        }
    }
}
